package com.zfzs.post.modules.excel.service.impl;

import com.alibaba.fastjson.JSON;
import com.zfzs.post.modules.excel.domain.SysUploadExcel;
import com.zfzs.post.modules.excel.repository.SysUploadExcelRepository;
import com.zfzs.post.modules.excel.service.SysUploadExcelService;
import com.zfzs.post.modules.excel.service.dto.SysUploadExcelQueryCriteria;
import com.zfzs.post.modules.excel.service.mapper.SysUploadExcelMapper;
import com.zfzs.post.modules.excel.vo.SysUploadExcelVO;
import com.zfzs.post.modules.system.service.UserService;
import com.zfzs.post.modules.system.service.dto.UserDTO;
import com.zfzs.post.utils.QueryHelp;
import com.zfzs.post.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.*;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

/**
 * 上传
 *
 * @author ck
 * @date 2019-12-04
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class SysUploadExcelServiceImpl implements SysUploadExcelService {

    @Autowired
    private SysUploadExcelRepository sysUploadExcelRepository;

    @Autowired
    private SysUploadExcelMapper sysUploadExcelMapper;

    @Autowired
    private UserService userService;

    @Override
    public Object queryAll(SysUploadExcelQueryCriteria criteria, Pageable pageable) {
        Map<String, Object> map = new HashMap<>();
        //获取渠道ID
        Long id = SecurityUtils.getUserId();
        //管理员数据
        if (!id.equals(1L)) {
            criteria.setChannelId(id);
        }
        Page<SysUploadExcel> page = sysUploadExcelRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        long totalElements = page.getTotalElements();
        List<SysUploadExcel> list = page.getContent();
        List<SysUploadExcelVO> voList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            SysUploadExcelVO vo = JSON.parseObject(JSON.toJSONString(list.get(i)), SysUploadExcelVO.class);
            //查询渠道
            UserDTO user = userService.findById(list.get(i).getChannelId());
            vo.setChannelName(user.getNickname());
            vo.setTypesName(list.get(i).getTypes().equals(1L) ? "终端" : "交易");
            voList.add(vo);
        }
        map.put("content", voList);
        map.put("totalElements", totalElements);
        return map;
    }

    @Override
    public Object queryAll(SysUploadExcelQueryCriteria criteria) {
        return sysUploadExcelRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));
    }

    @Override
    public SysUploadExcel findById(Integer id) {
        Optional<SysUploadExcel> sysUploadExcel = sysUploadExcelRepository.findById(id);
        return sysUploadExcel.get();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysUploadExcel create(SysUploadExcel resources) {
        resources.setCreateTime(new Timestamp(System.currentTimeMillis()));
        return sysUploadExcelRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysUploadExcel resources) {
        Optional<SysUploadExcel> optionalSysUploadExcel = sysUploadExcelRepository.findById(resources.getId());
        SysUploadExcel sysUploadExcel = optionalSysUploadExcel.get();
        sysUploadExcel.copy(resources);
        sysUploadExcelRepository.save(sysUploadExcel);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) {
        sysUploadExcelRepository.deleteById(id);
    }
}